/*
 *  txdnet.cn tonydon
 * 
 */
package cn.txdnet.txcms.admin.action;

import cn.txdnet.txcms.domain.HotWord;
import cn.txdnet.txcms.query.HotWordQuery;
import cn.txdnet.txcms.service.HotWordService;
import cn.txdnet.txweb.annotation.methods.PostMethod;
import cn.txdnet.txweb.model.ModelUtil;
import cn.txdnet.txweb.model.ModelView;
import cn.txdnet.txweb.utils.BeanUtil;
import cn.txdnet.txweb.utils.Message;
import cn.txdnet.txweb.utils.WebUtil;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author txdnet
 */
public class HotWordAction {

    /**
     * 保存新hotword
     *
     * @param req
     * @return
     * @throws Exception
     */
    @PostMethod
    public ModelView save(HttpServletRequest req) throws Exception {
        HotWord hotword = BeanUtil.parseRequestToBean(HotWord.class, req);
        if (!hotword.validate()) {
            return ModelUtil.getTextModel(Message.MSG_FIELD_ERR);
        }
        if (HotWordService.make(hotword)) {
            return ModelUtil.getTextModel(Message.MSG_SUCCESS);
        } else {
            return ModelUtil.getTextModel(Message.MSG_FAIL);
        }
    }

    /**
     * 批量删除hotword
     *
     * @param req
     * @return
     */
    public ModelView delete_batch(HttpServletRequest req) {
        String ids = WebUtil.getArrStr(req, "ids", null);
        if (ids == null) {
            return ModelUtil.getTextModel(Message.MSG_PARAM_ERR);
        }
        int del_num = HotWordService.deleteBatch(ids);
        if (del_num > 0) {
            return ModelUtil.getTextModel(del_num);
        } else {
            return ModelUtil.getTextModel(Message.MSG_FAIL);
        }
    }

    /**
     * 批量更新发布时间
     *
     * @param req
     * @return
     */
    public ModelView update_pubtime(HttpServletRequest req) {
        String ids = WebUtil.getArrStr(req, "ids", null);
        long pubtime = WebUtil.getLong(req, "pubtime", -1);
        if (ids == null || pubtime < 0) {
            return ModelUtil.getTextModel(Message.MSG_PARAM_ERR);
        }
        int num = HotWordService.updatePubTime(ids, pubtime);
        if (num > 0) {
            return ModelUtil.getTextModel(num);
        } else {
            return ModelUtil.getTextModel(Message.MSG_FAIL);
        }
    }

    /**
     * 批量更新停止时间
     *
     * @param req
     * @return
     */
    public ModelView update_stoptime(HttpServletRequest req) {
        String ids = WebUtil.getArrStr(req, "ids", null);
        long stoptime = WebUtil.getLong(req, "stoptime", -1);
        if (ids == null || stoptime < 0) {
            return ModelUtil.getTextModel(Message.MSG_PARAM_ERR);
        }
        int num = HotWordService.updateStopTime(ids, stoptime);
        if (num > 0) {
            return ModelUtil.getTextModel(num);
        } else {
            return ModelUtil.getTextModel(Message.MSG_FAIL);
        }
    }

    /**
     * 批量更新类目
     *
     * @param req
     * @return
     */
    public ModelView update_catelog(HttpServletRequest req) {
        String ids = WebUtil.getArrStr(req, "ids", null);
        int cid = WebUtil.getInt(req, "cid", -1);
        String ctp = WebUtil.getNoNull(req, "ctp", null);
        if (ids == null || cid < 0 || ctp == null) {
            return ModelUtil.getTextModel(Message.MSG_PARAM_ERR);
        }
        int num = HotWordService.updateCatalogBatch(ids, cid, ctp);
        if (num > 0) {
            return ModelUtil.getTextModel(num);
        } else {
            return ModelUtil.getTextModel(Message.MSG_FAIL);
        }
    }

    /**
     * 更新顺序号
     *
     * @param req
     * @return
     */
    public ModelView update_ordernum(HttpServletRequest req) {
        long id = WebUtil.getLong(req, "id", -1);
        long ordernum = WebUtil.getLong(req, "ordernum", -1);
        if (id < 0 || ordernum < 0) {
            return ModelUtil.getTextModel(Message.MSG_PARAM_ERR);
        }
        if (HotWordService.updateOrderNum(id, ordernum)) {
            return ModelUtil.getTextModel(Message.MSG_SUCCESS);
        } else {
            return ModelUtil.getTextModel(Message.MSG_FAIL);
        }
    }

    /**
     * 更新hotword
     *
     * @param req
     * @return
     * @throws Exception
     */
    @PostMethod
    public ModelView update(HttpServletRequest req) throws Exception {
        HotWord hotword = BeanUtil.parseRequestToBean(HotWord.class, req);
        if (!hotword.validate()) {
            return ModelUtil.getTextModel(Message.MSG_FIELD_ERR);
        }
        if (HotWordService.updateHotWord(hotword)) {
            return ModelUtil.getTextModel(Message.MSG_SUCCESS);
        } else {
            return ModelUtil.getTextModel(Message.MSG_FAIL);
        }
    }

    /**
     * 查询最大排序号
     *
     * @return
     */
    public ModelView query_max_ordernum() {
        return ModelUtil.getTextModel(HotWordService.queryMaxOrderNum());
    }

    /**
     * 查询hotword用于编辑(目前直接在列表中保存更新，此方法暂不使用)
     *
     * @param req
     * @return
     * @throws Exception
     */
    public ModelView query_hotword_edit(HttpServletRequest req) throws Exception {
        long id = WebUtil.getLong(req, "id", -1);
        if (id < 0) {
            return ModelUtil.getTextModel(Message.MSG_FIELD_ERR);
        }
        HotWord hotword = HotWordService.queryHotWordForEdit(id);
        if (hotword == null) {
            return ModelUtil.getTextModel(Message.MSG_VALUE_NULL);
        }
        return ModelUtil.getJsonModel(hotword);
    }

    /**
     * 分页查询
     *
     * @param req
     * @return
     * @throws Exception
     */
    public ModelView list_factor(HttpServletRequest req) throws Exception {
        HotWordQuery query = BeanUtil.parseRequestToBean(HotWordQuery.class, req);
        return ModelUtil.getJsonModel(HotWordService.queryByFactor(query));
    }
}
